package leetcode.code2055;

import java.util.TreeMap;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {

	char plate = '*';

	public int[] platesBetweenCandles(String s, int[][] queries) {
		char[] cs = s.toCharArray();
		int len = cs.length;
		TreeMap<Integer, Integer> tm = new TreeMap<>();
		int plateNum = 0;
		for (int i = 0; i < len; i++) {
			char cur = cs[i];
			if (cur == plate) {
				plateNum++;
			} else {
				tm.put(i, plateNum);
			}
		}
		int size = queries.length;
		int[] ans = new int[size];
		for (int i = 0; i < size; i++) {
			int[] query = queries[i];
			Integer key1 = tm.floorKey(query[1]);
			Integer key2 = tm.ceilingKey(query[0]);
			if (key1 != null && key2 != null) {
				int cur = tm.get(key1) - tm.get(key2);
				ans[i] = cur > 0 ? cur : 0;
			}
		}
		return ans;
	}

	@Override
	public void debug1() {
		int[] ans = HelpDebug.str2array3("[2,3]");
		HelpDebug.compare(ans, this.platesBetweenCandles("**|**|***|", HelpDebug.str2array1("[[2,5],[5,9]]")));
	}

	@Override
	public void debug2() {

		// TODO Auto-generated method stub
		int[] ans = HelpDebug.str2array3("[2,3]");
		HelpDebug.compare(ans, this.platesBetweenCandles("||*", HelpDebug.str2array1("[[2,2]]")));
	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug2();
		so.debug1();
		so.debug3();
		so.debug4();

	}

}
